<template>
  <div class="login_container">
    <div class="login_box">
      <div class="logo">
        <img
          src="../assets/logo.jpg"
          alt=""
          srcset=""
        >
      </div>
      <el-form
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
        label-width="0"
        class="form_con"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
            placeholder="请输入用户名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="iconfont icon-3702mima"
            placeholder="请输入密码"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item class="text_right">
          <el-button
            type="primary"
            @click="loginSubmitForm('loginFormRef')"
          >登录</el-button>
          <el-button
            type="info"
            @click="loginResetFrom('loginFormRef')"
          >重置</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 登录
    loginSubmitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) return
        console.log(valid)
        let { data: res } = await this.$axios.post('login', this.loginForm)
        console.log(res)
        // 登录失败
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        // 登录成功
        this.$message.success(res.meta.msg)
        // 保存登录状态
        window.sessionStorage.setItem('token', res.data.token)
        // 跳转路由
        this.$router.push('/home')
      })
    }, 
    loginResetFrom(formName) {
      // 重置表单
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.login_container {
  height: 100%;
  background-color: #2b4b6b;
}
.login_box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 500px;
  height: 300px;
  background-color: #fff;
}
.logo {
  width: 100px;
  height: 100px;
  overflow: hidden;
  border-radius: 50%;
  padding: 5px;
  box-sizing: border-box;
  background-color: #fff;
  border: 2px solid #eee;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  img {
    width: 100%;
  }
}
.form_con {
  padding: 0 50px;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
}
</style>
